/**
 * 
 */
package com.yidu.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import com.yidu.dao.ModuleDao;
import com.yidu.dao.RoleDao;
import com.yidu.dao.RoleModuleDao;
import com.yidu.dao.RoleModuleDaoImp;
import com.yidu.dao.UserDao;
import com.yidu.domain.Module;
import com.yidu.domain.Role;
import com.yidu.domain.RoleModule;
import com.yidu.domain.User;
import com.yidu.domain.UserRole;
import com.yidu.domain.vo.RoleVo;
import com.yidu.domain.vo.UserVo;
import com.yidu.utils.Pages;

/**
 * @描述 角色的Service
 * @author 石嘉澄
 * @date 2019年9月26日
 */
public class RoleServiceImpl implements RoleService {
	
	private RoleDao roleDao;
	private ModuleDao moduleDao ;
	private RoleModuleDao roleModuleDao; 
	private UserDao userDao ;
	
	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	
	

	public ModuleDao getModuleDao() {
		return moduleDao;
	}

	public void setModuleDao(ModuleDao moduleDao) {
		this.moduleDao = moduleDao;
	}
	
	public RoleModuleDao getRoleModuleDao() {
		return roleModuleDao;
	}

	public void setRoleModuleDao(RoleModuleDao roleModuleDao) {
		this.roleModuleDao = roleModuleDao;
	}
	

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/**
	 * 新增
	 */
	public void add(RoleVo rolevo) {
		Role po = new Role();
		if(rolevo.getRoleValidity()==null) {
			rolevo.setRoleValidity("0");
		}
		else if(rolevo.getRoleValidity().equals("on")) {
			rolevo.setRoleValidity("1");
		}else {
			rolevo.setRoleValidity("0");
		}
		if(rolevo.getRolePeople()!=null && !rolevo.getRolePeople().equals("")) {
			User user = userDao.findById(rolevo.getRolePeople());
			rolevo.setRolePeople(user.getUserName());
		}
		BeanUtils.copyProperties(rolevo, po);
		roleDao.add(po);
		if(rolevo.getModuleId()!=null &&!"".equals(rolevo.getModuleId())) {
			String[] id = rolevo.getModuleId().split(",");
			for (int i = 0; i < id.length; i++) {
				Module module = moduleDao.findById(id[i]);
				RoleModule roleModule = new RoleModule();
				roleModule.setModule(module);
				roleModule.setRole(po);
				roleModuleDao.saveOrUpdate(roleModule);
			}
		}

	}

	/**
	 * 修改
	 */
	public void delete(String roleId) {
		roleDao.delete(roleId);
	}

	/**
	 * 修改
	 */
	public void update(RoleVo rolevo) {
		if(rolevo.getRoleValidity()==null) {
			rolevo.setRoleValidity("0");
		}
		else if(rolevo.getRoleValidity().equals("on")) {
			rolevo.setRoleValidity("1");
		}else {
			rolevo.setRoleValidity("0");
		}
		Role role = roleDao.findById(rolevo.getRoleId());
		rolevo.setRolePeople(role.getRolePeople());
		BeanUtils.copyProperties(rolevo, role);
		
		roleDao.update(role);
		if(rolevo.getRoleId()!=null && !"".equals(rolevo.getRoleId())) {
			List<RoleModule> list = roleModuleDao.findRoleId(rolevo.getRoleId());
			for (int i = 0; i < list.size(); i++) {
				roleModuleDao.delete(list.get(i).getRoleModuleId());
			}
		}
		if(rolevo.getModuleId()!=null &&!"".equals(rolevo.getModuleId())) {
			String[] id = rolevo.getModuleId().split(",");
			for (int i = 0; i < id.length; i++) {
				Module module = moduleDao.findById(id[i]);
				RoleModule roleModule = new RoleModule();
				roleModule.setModule(module);
				roleModule.setRole(role);
				roleModuleDao.saveOrUpdate(roleModule);
			}
		}
		
	}

	/**
	 * 批量删除
	 */
	public Integer deleteAll(String ids) {
		String temp[] = ids.split(",");
		int rows = 0;
		for (int i = 0; i < temp.length; i++) {
			String id = temp[i];
			if(StringUtils.isEmpty(id)) {
				continue ;
			}
			delete(id);
			rows++;
		}
		return rows;

	}

	/**
	 * 分页显示查询全部
	 */
	public List<RoleVo> findAllPage(RoleVo rolevo, Pages page) {
		Role role = new Role();
		BeanUtils.copyProperties(rolevo, role);
		List<Role> list = roleDao.findAllPage(role, page);
		List<RoleVo> listVo = new ArrayList<RoleVo>();
		for (Iterator<Role> iterator = list.iterator(); iterator.hasNext();) {
			Role po = (Role) iterator.next();
			RoleVo vo = new RoleVo();
			BeanUtils.copyProperties(po, vo);
			listVo.add(vo);
		}
		
		return listVo;
	}

	/**
	 * 根据ID查询
	 */
	public RoleVo findById(String roleId) {
		Role po = roleDao.findById(roleId);
		RoleVo vo = new RoleVo();
		if(vo.getRoleId()!=null) {
			vo.getRoleId();
		}
		BeanUtils.copyProperties(po, vo);
		return vo;
	}

	@Override
	public Long count(RoleVo vo) {
		// TODO Auto-generated method stub
		return roleDao.count(vo);
	}
	/**
	 * 查询全部不分页
	 */
	@Override
	public List<RoleVo> findAll(RoleVo rolevo) {
		Role role = new Role();
		BeanUtils.copyProperties(rolevo, role);
		List<Role> list = roleDao.findAll(role);
		List<RoleVo> listVo = new ArrayList<RoleVo>();
		for (Iterator<Role> iterator = list.iterator(); iterator.hasNext();) {
			Role po = (Role) iterator.next();
			RoleVo vo = new RoleVo();
			BeanUtils.copyProperties(po, vo);
			listVo.add(vo);
		}
		
		return listVo;
	}

	@Override
	public void updateRolevalidity(String roleId, String roleValidity) {
		roleDao.updateRolevalidity(roleId, roleValidity);
	}

}
